<?php
	class LoaiPhong extends CI_Controller
	{
		function admin_index()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{		
				$this->load->model('loaiphong_model');		
				$data['loaiphong'] = $this->loaiphong_model->LayTatCaLoaiPhong_KhachSan();
								
				$this->load->model('khachsan_model');
				$data['quocgia'] = $this->khachsan_model->LayTatCaQuocGia();
								
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/loaiphong_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function Tim()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = urldecode($this->uri->segment(3));
				
				$this->load->model('loaiphong_model');
				$data['loaiphong'] = $this->loaiphong_model->Tim($key);
								
				$this->load->model('khachsan_model');
				$data['quocgia'] = $this->khachsan_model->LayTatCaQuocGia();				
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/loaiphong_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function LayLoaiPhong_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('loaiphong_model');
				$data['malp'] = $this->loaiphong_model->LayMaLP_ajax($key);
				$data['tenlp'] = $this->loaiphong_model->LayTenLP_ajax($key);
				$data['tenkhachsan'] = $this->loaiphong_model->LayTenKhachSan_ajax($key);												
				
				$this->load->view('admin/ajax_danhsachloaiphong', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function Xoa()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$malp = $this->uri->segment(3);
				if($malp)
				{
					$this->load->model('loaiphong_model');
					$result = $this->loaiphong_model->Xoa($malp);
				
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	            	    <script>
							alert("Loại phòng đã được xóa!");
							window.location = "<?php echo base_url(); ?>index.php/loaiphong/admin_index";
						</script>
	                	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	    	            <script>
							alert("Xóa loại phòng thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/loaiphong/admin_index";
						</script>
	    	            <?php
					}			
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function MoTrangCapNhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$malp = $this->uri->segment(3);
				if($malp)
				{
					$this->load->model('loaiphong_model');
					$this->load->model('khachsan_model');
										
					$maks = $this->loaiphong_model->LayMaKS($malp);
					$quocgia = $this->khachsan_model->LayTenQuocGia($maks);
					$thanhpho = $this->khachsan_model->LayTenThanhPho($maks);
					
					$data['quocgia'] = $this->khachsan_model->LayTatCaQuocGia();
					$data['thanhpho'] = $this->khachsan_model->LayThanhPho($quocgia);		
					$data['khachsan'] = $this->khachsan_model->LayKhachSan($quocgia, $thanhpho);
					
					$data['loaiphong'] = $this->loaiphong_model->LayLoaiPhong_KhachSan($malp);
															
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');
					
					$this->load->view('admin/capnhat_loaiphong_view', $data);
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function LayThanhPho()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$quocgia = $this->input->post('quocgia');

				if($quocgia)
				{
					$this->load->model('khachsan_model');		
					$data['thanhpho'] = $this->khachsan_model->LayThanhPho($quocgia);
				
					$this->load->view('admin/ajax_thanhpho_loaiphong', $data);
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function LayKhachSan()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$quocgia = $this->input->post('quocgia');				
				$thanhpho = $this->input->post('thanhpho');

				if($thanhpho)
				{
					$this->load->model('khachsan_model');		
					$data['select_khachsan'] = $this->khachsan_model->LayKhachSan($quocgia, $thanhpho);
				
					$this->load->view('admin/ajax_khachsan_loaiphong', $data);
				}
			}
			else
				redirect('login/admin_index');			
		}
						
		function Them()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$loaiphong = array(
									'Ten' => $this->input->post('ten'),
									'GiaTruocKhuyenMai' => $this->input->post('giathue') / 1000,
									'SoDemToiThieu' => $this->input->post('sodem'),
									'SoNguoiLonMacDinh' => $this->input->post('songuoimacdinh'),
									'SoNguoiLonToiDa' => $this->input->post('songuoilon'),
									'SoNguoiToiDa' => $this->input->post('songuoi'),
									'SoTienThemNguoiHienHanh' => $this->input->post('sotienthem') / 1000,
									'KhachSan' => $this->input->post('khachsan')
								);
								
				$this->load->model('loaiphong_model');
				$result = $this->loaiphong_model->Them($loaiphong);
				
				if($result)
				{
					echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
					?>
            	    <script>
						alert("Loại phòng đã được thêm!");
						window.location = "<?php echo base_url(); ?>index.php/loaiphong/admin_index";
					</script>
                	<?php
				}
				else
				{
					echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
					?>
    	            <script>
						alert("Thêm loại phòng thất bại!");
						window.location = "<?php echo base_url(); ?>index.php/loaiphong/admin_index";
					</script>
    	            <?php
				}			
			}
			else
				redirect('login/admin_index');			
		}
		
		function CapNhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$malp = $this->input->post('malp');
				$giathue = $this->input->post('giathue') / 1000;
				$data = array(
									'Ten' => $this->input->post('ten'),
									'GiaTruocKhuyenMai' => $giathue,
									'SoDemToiThieu' => $this->input->post('sodem'),
									'SoNguoiLonMacDinh' => $this->input->post('songuoimacdinh'),
									'SoNguoiLonToiDa' => $this->input->post('songuoilon'),
									'SoNguoiToiDa' => $this->input->post('songuoi'),
									'SoTienThemNguoiHienHanh' => $this->input->post('sotienthem') / 1000,
									'KhachSan' => $this->input->post('khachsan')
								);
								
				$this->load->model('loaiphong_model');
				$result = $this->loaiphong_model->CapNhat($data, $malp);
				
				if($result < 0)
				{
					echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
					?>
            	    <script>
						alert("Thay đổi thông tin thất bại!");
						window.location = "<?php echo base_url(); ?>index.php/loaiphong/MoTrangCapNhat/<?php echo $malp; ?>";
					</script>
                	<?php
					return;
				}
				else
				{
					$maks = $this->loaiphong_model->LayMaKS($malp);
				
					$this->load->model('thongtinkhuyenmai_model');				
					$thongtinkhuyenmai = $this->thongtinkhuyenmai_model->LayKhuyenMai_CuaMotKhachSan($maks);
				
					$today = date("Y-m-d");
					$this->load->model('phongtrongtheongay_model');				
					$phongtrong = $this->phongtrongtheongay_model->LayPhongTrong_CuaMotLoaiPhong_TheoNgay($today, $malp);
				
					if($phongtrong)
					{
						foreach($phongtrong as $row_1)
						{
							$mapt = $row_1->MaPT;
							$ngay = $row_1->Ngay;
						
							$giasaukhuyenmai = $giathue;
	
							if($thongtinkhuyenmai)
							{
								foreach($thongtinkhuyenmai as $row_2)
								{
									$giatrikhuyenmai = $row_2->GiaTriKhuyenMai;
									$ngaybatdau = $row_2->NgayBatDau;
									$ngayketthuc = $row_2->NgayKetThuc;
							
									if($ngay >= $ngaybatdau && $ngay <= $ngayketthuc)
										$giasaukhuyenmai = $giathue - $giathue*($giatrikhuyenmai/100);
								}
							}
						
							$data_phongtrong = array('GiaSauKhuyenMai' => $giasaukhuyenmai);
							$result = $this->phongtrongtheongay_model->CapNhat($data_phongtrong, $mapt);
								
							if($result < 0)
							{
								echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
								?>
							    <script>
									alert("Thay đổi thông tin thất bại!");
									window.location = "<?php echo base_url(); ?>index.php/loaiphong/MoTrangCapNhat/<?php echo $malp; ?>";
								</script>
							   	<?php
							}
							else
							{
								echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
								?>
		    	    	        <script>
									alert("Thông tin đã được thay đổi!");
									window.location = "<?php echo base_url(); ?>index.php/loaiphong/MoTrangCapNhat/<?php echo $malp; ?>";
								</script>
			    	            <?php
							}
						}
					}
				}				
			}
			else
				redirect('login/admin_index');			
		}
	}
?>